import {
  areaData,
  areaListData
} from '../utils/location';
console.log(areaData);
console.log(areaListData);
Page({
  data: {
    areaListData: areaListData,
    areaData: areaData,
    regionData: [],
    chosedList: {
      area: '',
      areaCoding: '',
      city: '',
      cityCoding: '',
      province: '',
      provinceCoding: '',
    },
  },
  onLoad(options) {
    // let e = {}
    let e = {
      code: ["350000", "350200", "350206"],
      region: ["重庆市", "重庆市", "江北区"]
    }
    let chose = this.data.chosedList
    let data = [
      [],
      [],
      []
    ]
    let index = []
    let chosedData = {}
    data[0] = this.data.areaListData.areaSelect[0]
    // 有无初始值
    if (e.code && e.code.length == 3) {
      this.data.areaData.map((item, inde) => {
        if (e.region[0] == item.areaName) {
          chosedData = item
          chose.provinceCoding = item.coding
          chose.province = item.areaName
          index[0] = inde
          item.childrenList.length > 0 && item.childrenList.map((child, ind) => {
            data[1].push(child.areaName)
            chose.city = child.coding
            chose.cityCoding = child.areaName
            if (e.region[1] == child.areaName) {
              index[1] = ind
              child.childrenList.length > 0 && child.childrenList.map((son, indexs) => {
                data[2].push(son.areaName)
                chose.areaCoding = son.coding
                chose.area = son.areaName
                if (e.region[2] == son.areaName) {
                  index[2] = indexs
                }
              })
            }
          })
        }
      })
    } else {
      data[0] = this.data.areaListData.areaSelect[0]
      chosedData = this.data.areaData[0]
      this.data.areaData.map((item) => {
        if (data[0][0] == item.areaName) {
          chose.provinceCoding = item.coding
          chose.province = item.areaName
          item.childrenList.length > 0 && item.childrenList.map((child) => {
            data[1].push(child.areaName)
            chose.city = child.coding
            chose.cityCoding = child.areaName
            if (data[1][0] == child.areaName) {
              child.childrenList.length > 0 && child.childrenList.map((son) => {
                data[2].push(son.areaName)
                chose.areaCoding = son.coding
                chose.area = son.areaName
              })
            }
          })
        }
      })
    }

    this.setData({
      chosedList: chose,
      multiIndex: index,
      regionData: data,
      chosedData: chosedData
    })
  },
  bindMultiPickerColumnChange(e) {
    let data = this.data.regionData
    let str = []
    if (e.detail.column === 0) {
      let arr = this.data.areaData[e.detail.value]
      data[1] = []
      data[2] = []
      str = [e.detail.value, 0, 0]
      this.setData({
        chosedData: arr
      })

      this.data.chosedData.childrenList.length > 0 && this.data.chosedData.childrenList.map((item) => {
        data[1].push(item.areaName)
        if (data[1][0] == item.areaName) {
          item.childrenList.length > 0 && item.childrenList.map((son) => {
            data[2].push(son.areaName)
          })
        }
      })
    }
    if (e.detail.column === 1) {
      data[2] = []
      str = [this.data.multiIndex[0], e.detail.value, 0]
      let item = this.data.chosedData.childrenList[e.detail.value]
      item.childrenList.length > 0 && item.childrenList.map((item) => {
        data[2].push(item.areaName)
      })
    }
    if (e.detail.column === 2) {
      str = [this.data.multiIndex[0], this.data.multiIndex[1], e.detail.value]
    }
    this.setData({
      regionData: data,
      multiIndex: str
    })
  },
  bindMultiPickerChange(e) {
    this.setData({
      multiIndex: e.detail.value
    })
  },
  save() {
    let data = this.data.chosedData
    let chose = {}
    if (this.data.regionData.length === 3) {
      chose = {
        area: data.childrenList[this.data.multiIndex[1]].childrenList[this.data.multiIndex[2]].areaName,
        areaCoding: data.childrenList[this.data.multiIndex[1]].childrenList[this.data.multiIndex[2]].coding,
        city: data.childrenList[this.data.multiIndex[1]].areaName,
        cityCoding: data.childrenList[this.data.multiIndex[1]].coding,
        province: data.areaName,
        provinceCoding: data.coding,
      }
    } else {
      wx.showToast({
        title: '请选择地区',
        icon: 'none',
        duration: 1500,
        mask: false
      });
      return
    }
   console.log(chose)
  },
})